
import {
    CreateProduct,
    CreateProductSku,
    GetTagList,
    GetFirstAndSecondList,
    GetProductCategoryList,
    GetProductCategoryListByPid,
    GetProductSkuList,
    GetProductSkuValueList,
    GetProductById,
    CreateProductSkuValue,
    UpdateProduct,
    GetProductCategoryListById
} from '@/services/goods';
import {catchException} from '@/utils/utils';

export default {
    namespace: 'goods',
    state: {},
    effects: {
        // 发布商品
        *CreateProduct({ payload, callBack }, { call, put }) {
            const data = yield call(CreateProduct, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 编辑商品
        *UpdateProduct({ payload, callBack }, { call, put }) {
            const data = yield call(UpdateProduct, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 获取商品标签列表
        *GetTagList({ callBack }, { call, put }) {
            const data = yield call(GetTagList);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 获取一级+二级分类列表
        *GetFirstAndSecondList({ callBack }, { call, put }) {
            const data = yield call(GetFirstAndSecondList);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 获取商品顶级类目
        *GetProductCategoryList({ callBack }, { call, put }) {
            const data = yield call(GetProductCategoryList);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 获取商品子集类目
        *GetProductCategoryListByPid({ payload, callBack }, { call, put }) {
            const data = yield call(GetProductCategoryListByPid, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 获取商品规格列表
        *GetProductSkuList({ callBack }, { call, put }) {
            const data = yield call(GetProductSkuList);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 获取商品规格值列表
        *GetProductSkuValueList({ payload, callBack }, { call, put }) {
            const data = yield call(GetProductSkuValueList, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        *GetProductSkuValueListByZent({ payload, callBack }, { call, put }) {
            const data = yield call(GetProductSkuValueList, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack(data);
                }
            })
        },
        // 根据商品ID获取商品详情
        *GetProductById({ payload, callBack }, { call, put }) {
            const data = yield call(GetProductById, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        // 商品类目编辑列表初始化
        *GetProductCategoryListById({ payload, callBack }, { call, put }) {
            const data = yield call(GetProductCategoryListById, payload);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })
        },
        *CreateProductSku({ params, callBack }, { call, put }) {

            const data = yield call(CreateProductSku, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack(data);
                }
            })
        },
        *CreateProductSkuValue({ params, callBack }, { call, put }) {

            const data = yield call(CreateProductSkuValue, params);
            yield catchException(function* (){
                if (data.Code === 0) {
                    callBack && callBack(data);
                }
            })
        },
    },
    reducers: {

    }
}